package com.example.demo.zaqizabatest;

import com.google.gson.Gson;
import io.jsonwebtoken.Claims;
import io.jsonwebtoken.Jws;
import io.jsonwebtoken.JwtBuilder;
import io.jsonwebtoken.Jwts;
import io.jsonwebtoken.gson.io.GsonDeserializer;
import io.jsonwebtoken.security.Keys;
import org.apache.commons.codec.digest.DigestUtils;

import javax.crypto.SecretKey;
import java.io.IOException;
import java.nio.charset.StandardCharsets;

/**
 * @ClassName Jwt
 * @Author lys4134
 * @Date 2021/05/08 10:23:28
 * @Description Jwt
 * @Version 1.0
 **/
@SuppressWarnings("AlibabaLowerCamelCaseVariableNaming")
public class Jwt {
    public static void main(String[] args) throws IOException {
//        Long start = System.currentTimeMillis();
//        System.out.println();
//        for (int i = 0; i < 50000; i++){
//            System.out.println(i);
//        }
//        Long end = System.currentTimeMillis();
//        System.out.println((end - start)/1000);



//
//        // create
//        String secretKey = "rhEVbAWTwAOJiC+MqbkD9Q==";
//        String secretKey256 = DigestUtils.sha256Hex(secretKey); //sha256 保证key有足够的长度
//        System.out.println(secretKey256);
//        SecretKey key = Keys.hmacShaKeyFor(secretKey256.getBytes(StandardCharsets.UTF_8));
//
////
//        String jws = Jwts.builder()
////                .claim("uuid", "1626923269385")
////                .claim("timestamp", 1626923269385L)
////                .claim("work_id", "20208156")
////                .claim("user_name", "丁仁鑫")
////                .claim("hospital_id", "HID0101")
////                .claim("hospital_name", "四川大学华西医院")
//                .claim("uuid", UUID.randomUUID().toString())
//                .claim("timestamp", System.currentTimeMillis())
//                .claim("work_id", "host")
//                .claim("user_name", "host")
//                .claim("hospital_id", "daigui")
//                .claim("hospital_name", "待归")
////        {
////            "uuid": "de22407a-ee56-42c1-9ff6-2b6a37b1e1ec",
////                "timestamp": 1621241526801,
////                "work_id": "ys007",
////                "user_name": "付彦江（测试）",
////                "hospital_id": "HID0101",
////                "hospital_name": "四川大学华西医院"
////        }
//                .signWith(key)
//                .compact();
//        System.out.println("jwt = \n" + jws);


//        System.out.println("jwts = \n" + get360Token("0123456789"));


        //  parse
        String secretKey_1 = "K99UvADcsXBIOiT7Ka6RtQ==";
        String secretKey256_1 = DigestUtils.sha256Hex(secretKey_1);
        SecretKey key_1 = Keys.hmacShaKeyFor(secretKey256_1.getBytes(StandardCharsets.UTF_8));
        try{
            Jws<Claims> jwsClaim = Jwts.parserBuilder()
                    .deserializeJsonWith(new GsonDeserializer<>(new Gson()))
                    .setSigningKey(key_1)
                    .build()
                    .parseClaimsJws("eyJhbGciOiJIUzUxMiJ9.eyJ1dWlkIjoiMTYyNzAwNDc1ODk4MiIsInRpbWVzdGFtcCI6MTYyNzAwNDc1ODk4Miwid29ya19pZCI6IjIwMjA4MTU2IiwidXNlcl9uYW1lIjoi5LiB5LuB6ZGrIiwiaG9zcGl0YWxfaWQiOiJISUQwMTAxIiwiaG9zcGl0YWxfbmFtZSI6IuWbm-W3neWkp-WtpuWNjuilv-WMu-mZoiJ9.89L6OX2zJ9UxqADq76WhXvdOplOPsYQpaIOW92X54y7W9QHjUd27fCsYAMj-cIP_sWYVS9Kk7U4JVku2WlXyzw");
            Claims body = jwsClaim.getBody();
            System.out.println(body);
        }catch (Exception ex){
            System.out.println(ex.getMessage());
        }

    }


    public static String get360Token(String secretKey) {

        SecretKey key = Keys.hmacShaKeyFor(DigestUtils.sha256Hex(secretKey).getBytes(StandardCharsets.UTF_8));

        JwtBuilder jws = Jwts.builder()
                .claim("uuid", "bbabff48-bff8-47d0-bf8b-57d1b23f0d37")
                .claim("timestamp", 1621301162795L)
                .claim("work_id", "ys007")
                .claim("user_name", "付彦江（测试）")
                .claim("hospital_id", "HID0101")
                .claim("hospital_name", "四川大学华西医院")
                .signWith(key);

        return jws.compact();
    }


//    String jwt = JwtTokenUtils360.get360Token(projProperties.getHisJwtSecretKey(),doctorHisInfoVO.getDoctorCode(),
//            reqVO.getDoctorName(), SystemConstants.HX_ORGAN_CODE, SystemConstants.HX_ORGAN_NAME);
//
//    hisJwtSecretKey: 0123456789
//    hospital_id: HID0101
//    hospital_name: 四川大学华西医院

}
